package cn.tth.test;

import cn.tth.test.filter.TestGatewayFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: GatewayApplication
 * @Description:
 * @Author: yyh
 * @Date: 2020/8/27 11:52
 * @Version: 1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Autowired
    private TestGatewayFilter testGatewayFilter;

    @Bean
    public RouteLocator getRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes().
                //需要拦截的路径
                        route(r -> r.path("/test/**")
                        //去掉第一个前缀 /test
                        .filters(f -> f.stripPrefix(1)
                                .filter(testGatewayFilter)
                        )
                        //路由的路径 queen-test(服务名)
                        .uri("lb://queen-test"))
                .build();
    }

    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
                Map<String,String> tokenMap = new HashMap<>(2);

                System.out.println(getFromHeader(exchange.getRequest(), "token",tokenMap));

                return chain.filter(exchange);
            }
        };
    }

    private  String getFromHeader(ServerHttpRequest request, String key,Map<String,String> tokenMap) {
        List list = new ArrayList();
        request.getBody().subscribe(buffer -> {
            CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
            String formData = charBuffer.toString();
            String token = formData.substring(formData.indexOf("token"),formData.indexOf("----------------------------", 1) < 0 ? formData.indexOf("}"):formData.indexOf("----------------------------", 1)).replace("\n","");
            token = token.replace("\"", "");
            tokenMap.put("token",token.substring(token.indexOf("\"")+1));
            list.add(token);
        });
        list.forEach(o -> System.out.println(o));
        return tokenMap.get("token");
    }

}
